# 
# Created by francklinson on 2021/7/29 AT 23:05.
#
from typing import List


# 给定两个数组，编写一个函数来计算它们的交集。


class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        s1 = set(nums1)
        s2 = set(nums2)
        ans = []
        for x in s2:
            if x in s1:
                ans.append(x)
        return ans


class Solution2:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        set1 = set(nums1)
        set2 = set(nums2)
        return self.set_intersection(set1, set2)

    def set_intersection(self, set1, set2):
        if len(set1) > len(set2):
            return self.set_intersection(set2, set1)
        return [x for x in set1 if x in set2]


if __name__ == '__main__':
    sol = Solution2()
    print(sol.intersection(nums1=[1, 2, 2, 1], nums2=[2, 2]))
    print(sol.intersection(nums1=[4, 9, 5], nums2=[9, 4, 9, 8, 4]))
